﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Araman.Models.UserModels
{
    using Araman.Models;
    using System.Web.Mvc;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using Araman.Resources;

    // -- noldor -- 2011-05-29 -----------------------------------------------------
    // -----------------------------------------------------------------------------
    // Why double 'r' in Userr class name??
    // System.Web.Mvc namespace has already got a property named 'User'.
    // This current namespace and Web.Mvc namespace both could not be deleted from file headers, because they both will be used a lot in coding.
    // -----------------------------------------------------------------------------
    public class Userr
    {
        [Key()]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }

        [RegularExpression(@"[A-Za-z0-9_\-\.]+", ErrorMessageResourceName = "UserName_RegEx", ErrorMessageResourceType = typeof(LngModelErrors))]
        [MinLength(2, ErrorMessageResourceName = "UserName_MinLength", ErrorMessageResourceType = typeof(LngModelErrors))]
        [MaxLength(32, ErrorMessageResourceName = "UserName_MaxLength", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "UserName_Null", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Display(ResourceType = typeof(LngModelDisplayNames), Name = "Username")]
        public string UserName { get; set; }

        [Display(ResourceType = typeof(LngModelDisplayNames), Name = "User_RealName")]
        [MaxLength(256, ErrorMessageResourceName = "User_RealName_MaxLength", ErrorMessageResourceType = typeof(LngModelErrors))]
        public string RealName { get; set; }

        [DataType(DataType.EmailAddress, ErrorMessageResourceName = "Email_RegEx", ErrorMessageResourceType = typeof(LngModelErrors))]
        [MaxLength(256, ErrorMessageResourceName = "Email_MaxLength", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "Email_Null", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Display(ResourceType = typeof(LngModelDisplayNames), Name = "Email")]
        public string Email { get; set; }

        [MinLength(6, ErrorMessageResourceName = "Password_MinLength", ErrorMessageResourceType = typeof(LngModelErrors))]
        [MaxLength(32, ErrorMessageResourceName = "Password_MaxLength", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "Password_Null", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Display(ResourceType = typeof(LngModelDisplayNames), Name = "Password")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        public virtual UserGroup ParentGroup { get; set; }
        public virtual UserStatus Status { get; set; }
        public virtual LocalizationModels.Language InterfaceLanguage { get; set; }


        public void Login()
        {
            Userr.CurrentUser = this;
        }
        public void Logout()
        {
            Userr.CurrentUser = null;
        }
        public void ChangeUserStatus(UserStatus newStatus)
        {
            if (newStatus == null) return;
            this.Status = newStatus;
            DbModel.Current.SaveChanges();
        }
      
        [NotMapped()]
        public static Userr CurrentUser
        {
            get
            {
                string strUserId = "";
                uint userId = 0;
                if (HttpContext.Current.Session["CurrentUserId"] == null) return null;
                strUserId = HttpContext.Current.Session["CurrentUserId"].ToString();
                if (String.IsNullOrEmpty(strUserId)) return null;
                if (!UInt32.TryParse(strUserId, out userId)) return null;
                return (from u in DbModel.Current.Users where u.UserId == userId select u).FirstOrDefault<Models.UserModels.Userr>();
            }
            set
            {
                if (value == null)
                {
                    HttpContext.Current.Session["CurrentUserId"] = null;
                    HttpContext.Current.Session.Remove("CurrentUserId");
                    return;
                }
                HttpContext.Current.Session["CurrentUserId"] = value.UserId;
                return;
            }
        }
    }
}